package gw.com.android.ui.news;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import butterknife.BindView;
import gw.com.android.app.GTConfig;
import gw.com.android.presenter.BackTopPresenter;
import gw.com.android.presenter.http.StrategyRequest;
import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.onlive.FastScrollLinearLayoutManager;
import gw.com.android.utils.AppTest;
import gw.com.android.utils.ENV;
import www.com.library.interfaces.ReqCallBack;
import www.com.library.util.DeviceUtil;
import www.com.library.view.XRecyclerView;

/**
 * 交易策略列表
 *
 * @author jett
 * @since 2017-11-28.
 */
public class StrategyNewsActivity extends BaseActivity {

    private final String PREF_ARITICLE_NEW_TAG = "pref_ariticle_new_tag";

    @BindView(R.id.rv_news)
    XRecyclerView xListView;
    @BindView(R.id.ll_error)
    View errorView;
    @BindView(R.id.rl_empty)
    View emptyView;
    @BindView(R.id.iv_backToTop)
    View iv_backToTop;

    private StrategyRequest request;
    private StrategyNewsAdapter mAdapter;

    @Override
    protected int getLayoutView() {
        return R.layout.activity_strategy_news;
    }

    @Override
    protected void initLayoutView() {
        mTitleBar.setAppTitle(R.string.home_strategy_title);
        mTitleBar.mTitleView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (!ENV.isRelease()) {
                    new AppTest().record(StrategyNewsActivity.this);
                }
                return false;
            }
        });
        mAdapter = new StrategyNewsAdapter(this);
        xListView.setLayoutManager(new FastScrollLinearLayoutManager(this));
        xListView.setAdapter(mAdapter);
        xListView.setLoadingMoreEnabled(true);
        xListView.hideFooter();
        xListView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData();
            }

            @Override
            public void onLoadMore() {
                nextPage();
            }
        });
        int x = DeviceUtil.instance().dip2px(15, this);
        int y = DeviceUtil.instance().dip2px(36, this);
        new BackTopPresenter(this, xListView).location(Gravity.BOTTOM | Gravity.RIGHT, x, y).calcNavigationBar(true);
    }

    @Override
    protected void initViewData() {
        xListView.setRefreshing(true);
    }

    private void refreshUi(List<StrategyRequest.ArticleData> list) {
        errorView.setVisibility(View.GONE);
        if (list == null || list.isEmpty()) {
            emptyView.setVisibility(mAdapter.getItemCount() <= 0 ? View.VISIBLE : View.GONE);
            return;
        }
        StrategyRequest.ArticleData data = list.get(0);
        if (data == null) {
            return;
        }
        String value = GTConfig.instance().getStringValue(PREF_ARITICLE_NEW_TAG);
        if (!TextUtils.isEmpty(value) && value.equals(data.id)) {
            mAdapter.update(list, false);
        } else {
            GTConfig.instance().setStringValue(PREF_ARITICLE_NEW_TAG, data.id);
            mAdapter.update(list, true);
        }
        emptyView.setVisibility(mAdapter.getItemCount() <= 0 ? View.VISIBLE : View.GONE);
        if (mAdapter.getItemCount() > 0) {
            xListView.setNoMore(!request.hasNextPage());
        }
    }

    private void onError() {
        emptyView.setVisibility(View.GONE);
        errorView.setVisibility(mAdapter.getItemCount() <= 0 ? View.VISIBLE : View.GONE);
    }

    private void loadData() {
        if (request == null) {
            request = new StrategyRequest();
        }
        request.getList(new ReqCallBack<List<StrategyRequest.ArticleData>>() {
            @Override
            public void onReqSuccess(List<StrategyRequest.ArticleData> result) {
                xListView.refreshComplete();
                refreshUi(result);
            }

            @Override
            public void onReqFailed(String errorMsg) {
                xListView.refreshComplete();
                onError();
            }
        });
    }

    private void nextPage() {
        if (request == null) {
            request = new StrategyRequest();
        }
        request.nextPage(new ReqCallBack<List<StrategyRequest.ArticleData>>() {
            @Override
            public void onReqSuccess(List<StrategyRequest.ArticleData> result) {
                mAdapter.append(result);
                xListView.loadMoreComplete();
                xListView.setNoMore(!request.hasNextPage());
            }

            @Override
            public void onReqFailed(String errorMsg) {
                xListView.loadMoreComplete();
            }
        });
    }

}
